package com.insurance.controller;

import com.insurance.common.Contants;
import com.insurance.common.ResultData;
import com.insurance.service.OrderServcie;
import com.insurance.utils.EmailUtil;
import io.swagger.annotations.*;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: 朱雄
 * Date: 2020/10/26 20:51
 * Description:
 * Version: V1.0
 */
@Api(value = "订单管理",tags = "订单管理API")
@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private ResultData resultData;


    @Autowired
    private OrderServcie orderServcie;

    @ResponseBody
    @GetMapping(value = "/orderQuery.do",produces = "application/json;charset=utf-8")
    @ApiOperation(value = "查询订单",notes = "查询订单接口",httpMethod = "get",response = ResultData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="查询成功"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public ResultData queryOrder() throws SQLException {
      ResultData resultData=orderServcie.queryOrder();
      return resultData;
    }

    @ResponseBody
    @PostMapping(value = "/queryByMore.do",produces = "application/json;charset=utf-8")
    @ApiOperation(value = "订单多条件查询",notes = "订单多条件查询接口",httpMethod = "get",response = ResultData.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "city_id",value = "城市id",dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "user_phone",value = "用户电话",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "order_type_id",value = "订单类型id",dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "order_status_id",value = "订单状态id",dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "order_time",value = "订单开始时间",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "order_time_end",value = "订单结束时间",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "order_number",value = "订单号",dataType = "String",paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code=200,message="查询成功"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public ResultData queryByMore(@RequestBody Map map){
        resultData=orderServcie.queryByMore(map);
        return resultData;
    }
    
    @ResponseBody
    @GetMapping (value = "/sendUser.do",produces = "application/json;charset=utf-8")
    @ApiOperation(value = "给用户发送邮件",notes = "给用户发送邮件接口",httpMethod = "get",response = ResultData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="发送成功"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ApiImplicitParams({
    @ApiImplicitParam(name = "content",value = "发送内容",required = true,dataType = "String"),
    @ApiImplicitParam(name = "userEmali",value = "客户邮箱",required = true,dataType = "String")
    })
    public ResultData sendUser(String content,String userEmali){
        EmailUtil emailUtil=new EmailUtil(userEmali,content,"用户");
        emailUtil.run();
        resultData.setCode(Contants.SUCCESS);
        resultData.setMsg("发送成功");
        return resultData;
    }

    @ResponseBody
    @GetMapping(value = "/sendDriver.do",produces = "application/json;charset=utf-8")
    @ApiOperation(value = "给司机发送邮件",notes = "给司机发送邮件接口",httpMethod = "get",response = ResultData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="发送成功"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "content",value = "发送内容",required = true,dataType = "String"),
            @ApiImplicitParam(name = "userEmali",value = "客户邮箱",required = true,dataType = "String")
    })
    public ResultData sendDriver(String content,String userEmali){
        EmailUtil emailUtil=new EmailUtil(userEmali,content,"司机");
        emailUtil.run();
        resultData.setCode(Contants.SUCCESS);
        resultData.setMsg("发送成功");
        return resultData;
    }

    @PostMapping(value = "/downOrder.do",produces = "application/json;charset=utf-8")
    @ApiOperation(value = "导出查询的订单",notes = "导出查询的订单接口",httpMethod = "post",response = ResultData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="导出成功"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public ResultData downOrder(HttpServletResponse response) throws IOException, SQLException {
        ResultData resultData=orderServcie.queryOrder();
        List<Map> list= (List<Map>) resultData.getData();
        Workbook workbook = new XSSFWorkbook();
        //创建sheet
        Sheet sheet = workbook.createSheet("用户表");
        //创建表头
        Row row = sheet.createRow(0);
        row.createCell(0).setCellValue("订单编号");
        row.createCell(1).setCellValue("下单时间");
        row.createCell(2).setCellValue("用户姓名");
        row.createCell(3).setCellValue("联系电话");
        row.createCell(4).setCellValue("用户邮箱");
        row.createCell(5).setCellValue("订单类型");
        row.createCell(6).setCellValue("接车司机");
        row.createCell(7).setCellValue("接车司机所在区域");
        row.createCell(8).setCellValue("还车司机");
        row.createCell(9).setCellValue("还车司机所在区域");
        row.createCell(10).setCellValue("订单状态");
        row.createCell(11).setCellValue("特定事项");
        row.createCell(12).setCellValue("车牌号");
        row.createCell(13).setCellValue("发动机号");
        row.createCell(14).setCellValue("定损地点");
        row.createCell(15).setCellValue("登记人");
        row.createCell(15).setCellValue("接车地址");
        row.createCell(16).setCellValue("支付单号");
        row.createCell(17).setCellValue("支付方式");
        row.createCell(18).setCellValue("被保险人");
        Integer rowNum = 1;
        for (Map map : list){
            //循环创建每一行
            Row listRow = sheet.createRow(rowNum);
            if (map.get("order_number")!=null){
            listRow.createCell(0).setCellValue(map.get("order_number").toString());
            }
            if (map.get("user_name")!=null){
                listRow.createCell(2).setCellValue(map.get("user_name").toString());
            }
           if (map.get("user_phone")!=null){
               listRow.createCell(3).setCellValue(map.get("user_phone").toString());
            }
           if (map.get("user_email")!=null){
               listRow.createCell(4).setCellValue(map.get("user_email").toString());
            }
           if (map.get("order_type_name")!=null){
               listRow.createCell(5).setCellValue(map.get("order_type_name").toString());
            }
           if (map.get("pick_name")!=null){
               listRow.createCell(6).setCellValue(map.get("pick_name").toString());
            }
           if (map.get("pickdriverplace")!=null){
               listRow.createCell(7).setCellValue(map.get("pickdriverplace").toString());
            }
           if (map.get("alse_name")!=null){
               listRow.createCell(8).setCellValue(map.get("alse_name").toString());
            }
           if (map.get("alsodriverplace")!=null){
               listRow.createCell(9).setCellValue(map.get("alsodriverplace").toString());
            }
           if (map.get("order_status_name")!=null){
               listRow.createCell(10).setCellValue(map.get("order_status_name").toString());
            }
           if (map.get("specific_matters")!=null){
               listRow.createCell(11).setCellValue(map.get("specific_matters").toString());
            }
           if (map.get("car_license_num")!=null){
               listRow.createCell(12).setCellValue(map.get("car_license_num").toString());
            }
           if (map.get("car_engine_no")!=null){
               listRow.createCell(13).setCellValue(map.get("car_engine_no").toString());
            }
            if (map.get("fee_place")!=null){
                listRow.createCell(14).setCellValue(map.get("fee_place").toString());
            }
            if (map.get("registrar")!=null){
                listRow.createCell(15).setCellValue(map.get("registrar").toString());
            }
            if (map.get("pay_number")!=null){
                listRow.createCell(16).setCellValue(map.get("pay_number").toString());
            }
            if (map.get("pay_number")!=null){
                listRow.createCell(16).setCellValue(map.get("pay_number").toString());
            }
            if (map.get("pay_method")!=null){
                listRow.createCell(17).setCellValue(map.get("pay_method").toString());
            }
            if (map.get("insure_man")!=null){
                listRow.createCell(18).setCellValue(map.get("insure_man").toString());
            }
            rowNum++;
        }
        //写入目标文件
        //String targetFile = "C:\\Users\\浪人\\Desktop\\";
      //  File file = new File(targetFile,"用户统计表.xlsx");
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-disposition", "attachment;filename="+"订单统计表"+".xlsx");
        workbook.write(response.getOutputStream());
        response.getOutputStream().flush();
        response.getOutputStream().close();
        workbook.close();
        return null;
    }

    @ResponseBody
    @GetMapping(value = "/queryOrderType.do", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "查询订单类型",notes = "查询订单类型接口",httpMethod = "get",response = ResultData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="查询成功"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public ResultData queryOrderType() throws SQLException {
        ResultData resultData=orderServcie.queryOrderType();
        return resultData;
    }

    @ResponseBody
    @GetMapping(value = "/queryOrderStatus.do", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "查询订单状态",notes = "查询订单状态接口",httpMethod = "get",response = ResultData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="查询成功"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public ResultData queryOrderStatus() throws SQLException {
        ResultData resultData=orderServcie.queryOrderStatus();
        return resultData;
    }

}
